package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.mapper.CourseSchedulingAlwaysMapper;
import com.xmy.cultivate.mapper.CourseSchedulingMapper;
import com.xmy.cultivate.mapper.HolidayMapper;
import com.xmy.cultivate.mapper.StudentMapper;
import com.xmy.cultivate.respons.SchedulingGrade;
import com.xmy.cultivate.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.util.CommonUtil;
import com.xmy.cultivate.util.DateUtil;
import com.xmy.cultivate.util.IdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 排课表 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-03-20
 */
@Service
public class CourseSchedulingServiceImpl extends ServiceImpl<CourseSchedulingMapper, CourseScheduling> implements ICourseSchedulingService {

    @Autowired
    @Lazy
    ICourseSchedulingDetailService iCourseSchedulingDetailService;
    @Autowired
    @Lazy
    ICourseSchedulingRedoService iCourseSchedulingRedoService;

    @Autowired
    @Lazy
    IGradeService iGradeService;

    @Autowired
    @Lazy
    IOrganizationService iOrganizationService;

    @Autowired
    @Lazy
    IClassRoomService iClassRoomService;

    @Autowired
    @Lazy
    ICourseService iCourseService;

    @Autowired
    @Lazy
    IStaffService iStaffService;

    @Autowired
    @Lazy
    ISubjectsService iSubjectsService;

    @Autowired
    @Lazy
    CourseSchedulingMapper courseSchedulingMapper;

    @Autowired
    @Lazy
    StudentMapper studentMapper;

    @Autowired
    @Lazy
    IStudentGradeService iStudentGradeService;

    @Autowired
    @Lazy
    HolidayMapper holidayMapper;

    @Autowired
    @Lazy
    IDictService iDictService;

    @Autowired
    @Lazy
    ICourseSchedulingAlwaysService iCourseSchedulingAlwaysService;

    @Autowired
    @Lazy
    CourseSchedulingAlwaysMapper courseSchedulingAlwaysMapper;

    @Autowired
    @Lazy
    IdWorker idWorker;

    /**
     * 班级添加自动排课
     * @param gradeId
     * @param startDateParam 开始时间,不填写为班级开始时间，（班级修改时填写）
     * @return
     * @throws ParseException
     */
    @Override
    @Transactional
    public Integer autoScheduling(Long gradeId, LocalDate startDateParam) throws ParseException {

        Grade grade = iGradeService.getById(gradeId);
        //补课班与不自动排课直接返回
        if (grade.getIsMakeup().equals(1) || grade.getIsAutoScheduling().equals(0)){
            return 1;
        }


        if (grade == null)
            return 0;
        Integer isVacation = grade.getIsVacation();
        String weekDates = grade.getWeekDates();
        LocalDate startDate = this.getOpenDate(grade);

        if (startDateParam != null){
            //是否已经记上课过
            Integer count = iCourseSchedulingDetailService.getSchedulingCountForRecord(gradeId);
            if (count>0){
                startDate = startDateParam;
            }
        }
        LocalDate dateNow = LocalDate.now();
        //* 删除当前时间之后的排课
        iCourseSchedulingDetailService.deleteForGradeId(gradeId,dateNow);

        LocalDate endDate = courseSchedulingAlwaysMapper.getMaxEndDate(grade.getYearPart(),grade.getQuarterNum(),CommonUtil.getGradeSection(grade.getYearClassId().getKey()));
        if (endDate == null){
            endDate = grade.getEndDate();
        }else {
            if (endDate.isBefore(grade.getEndDate())){
                endDate = grade.getEndDate();
            }
        }
        //暑假班智能课与刷题班结束时间往后1天
        if (grade.getQuarterNum().equals(3) && !grade.getLessonType().equals(1)){
            //endDate = endDate.plusDays(1);
        }


        Organization organization = iOrganizationService.getById(grade.getSchoolId());
        Course course = iCourseService.getById(grade.getCourseId());
        Staff staff = iStaffService.getById(grade.getTeacherId());
        ClassRoom classRoom = iClassRoomService.getById(grade.getClassRoomId());

        String schoolName = organization.getName();
        String courseName = course.getName();
        String teacherName = staff.getName();
        String roomName = classRoom != null ? classRoom.getName() : "";
        Long subjectsId = course.getSubjectsId();
        String subjectsName = CommonUtil.getSubjectsNameForId(Integer.parseInt(subjectsId.toString()));
        Integer quarterNum = course.getQuarterNum();
        Long courseSchedulingId = idWorker.nextId();

        Integer schedulintCount = 0;
        Integer lessonNum = 1;

        //A天或者B天
        LocalDate nextDate = startDate;
        boolean isNext = true;
        //是否假期，0否，1是（假期指的是：寒假、暑假）
        if (isVacation.equals(1)) {
            Integer plusDay = 1;//寒假班加1，暑假班加2
            if (grade.getQuarterNum().equals(3)){
                plusDay = 2;//暑假班加2
            }
            while (isNext) {
                //是否存在节假日
                Integer isAt = holidayMapper.getIsAt(nextDate,1);
                if (isAt > 0) {
                    nextDate = nextDate.plusDays(plusDay);//加
                    continue;
                }
                boolean rs = this.addSchedulingForGrade(grade, schoolName, courseName, teacherName, roomName, subjectsId, subjectsName, quarterNum, nextDate, courseSchedulingId,lessonNum);
                if (rs) {
                    schedulintCount++;
                }
                nextDate = nextDate.plusDays(plusDay);//加
                if (nextDate.isAfter(endDate)) {
                    isNext = false;
                }
                lessonNum++;
            }

        } else {
            while (isNext) {
                Integer isAt = holidayMapper.getIsAt(nextDate,1);
                if (isAt > 0) {
                    //存在节假日
                    nextDate = nextDate.plusDays(1);//加1
                    continue;
                }
                Integer weekIndex = DateUtil.getWeekIndex(nextDate);//获取出当前属于星期几
                String[] stringArray = weekDates.split(",");
                List<Integer> weekDateList = new ArrayList<>();
                for (String str : stringArray) {
                    Integer number = Integer.parseInt(str);
                    weekDateList.add(number);
                }
                if (weekDateList.contains(weekIndex)) {
                    boolean rs = this.addSchedulingForGrade(grade, schoolName, courseName, teacherName, roomName, subjectsId, subjectsName, quarterNum, nextDate, courseSchedulingId,lessonNum);
                    if (rs) {
                        schedulintCount++;
                    }
                }
                nextDate = nextDate.plusDays(1);//加1
                if (nextDate.isAfter(endDate)) {
                    isNext = false;
                }
                lessonNum++;
            }
        }
        return schedulintCount;
    }

    public LocalDate getOpenDate(Grade grade){
        Course course = iCourseService.getById(grade.getCourseId());
        //前端调整了，防止已经存在的页面有缓存，添加下面代码
        Integer gradeSection = CommonUtil.getGradeSectionForYearClassId(course.getYearClassId().getKey());
        LocalDate openDate = iCourseSchedulingAlwaysService.getStartDate(grade.getYearPart(), course.getQuarterNum(), gradeSection, null);
        if (grade.getWeekDates().equals("B")) {
            openDate = openDate.plusDays(1);
        }
        if (grade.getQuarterNum().equals(3) && !grade.getLessonType().equals(1)){
            //智能或者刷题班
            //openDate = openDate.plusDays(1);
        }

        return openDate;
    }

    /**
     *
     * @param gradeId
     * @param startDate
     * @param endDate
     * @return
     * @throws ParseException
     */
    @Override
    @Transactional
    public Integer autoSchedulingForDate(Long gradeId, LocalDate startDate,LocalDate endDate) throws ParseException {

        Grade grade = iGradeService.getById(gradeId);
        if (grade == null)
            return 0;
        Integer isVacation = grade.getIsVacation();
        String weekDates = grade.getWeekDates();
        //* 删除当前时间之后的排课
        //iCourseSchedulingDetailService.deleteForGradeId(gradeId,startDate);

        Organization organization = iOrganizationService.getById(grade.getSchoolId());
        Course course = iCourseService.getById(grade.getCourseId());
        Staff staff = iStaffService.getById(grade.getTeacherId());
        ClassRoom classRoom = iClassRoomService.getById(grade.getClassRoomId());

        String schoolName = organization.getName();
        String courseName = course.getName();
        String teacherName = staff.getName();
        String roomName = classRoom != null ? classRoom.getName() : "";
        Long subjectsId = course.getSubjectsId();
        String subjectsName = CommonUtil.getSubjectsNameForId(Integer.parseInt(subjectsId.toString()));
        Integer quarterNum = course.getQuarterNum();
        Long courseSchedulingId = idWorker.nextId();

        Integer schedulintCount = 0;

        Integer lessonNum = 1;


        //A天或者B天
        LocalDate nextDate = startDate;
        boolean isNext = true;
        //是否假期，0否，1是（假期指的是：寒假、暑假）
        if (isVacation.equals(1)) {
            Integer plusDay = 1;//寒假班加1，暑假班加2
            if (grade.getQuarterNum().equals(3)){
                plusDay = 2;//暑假班加2
            }

            if (weekDates.equals("B")) {
                nextDate = nextDate.plusDays(1);//B天开始日期加1
            }
            while (isNext) {
                //是否存在节假日
                Integer isAt = holidayMapper.getIsAt(nextDate,1);
                if (isAt > 0) {
                    nextDate = nextDate.plusDays(plusDay);//加
                    continue;
                }
                boolean rs = this.addSchedulingForGrade(grade, schoolName, courseName, teacherName, roomName, subjectsId, subjectsName, quarterNum, nextDate, courseSchedulingId,lessonNum);
                if (rs) {
                    schedulintCount++;
                }
                nextDate = nextDate.plusDays(plusDay);//加
                if (nextDate.isAfter(endDate)) {
                    isNext = false;
                }
                lessonNum++;
            }

        } else {
            while (isNext) {
                Integer isAt = holidayMapper.getIsAt(nextDate,1);
                if (isAt > 0) {
                    //存在节假日
                    nextDate = nextDate.plusDays(1);//加1
                    continue;
                }
                Integer weekIndex = DateUtil.getWeekIndex(nextDate);//获取出当前属于星期几
                String[] stringArray = weekDates.split(",");
                List<Integer> weekDateList = new ArrayList<>();
                for (String str : stringArray) {
                    Integer number = Integer.parseInt(str);
                    weekDateList.add(number);
                }
                if (weekDateList.contains(weekIndex)) {
                    boolean rs = this.addSchedulingForGrade(grade, schoolName, courseName, teacherName, roomName, subjectsId, subjectsName, quarterNum, nextDate, courseSchedulingId,lessonNum);
                    if (rs) {
                        schedulintCount++;
                    }
                }
                nextDate = nextDate.plusDays(1);//加1
                if (nextDate.isAfter(endDate)) {
                    isNext = false;
                }
                lessonNum++;
            }
        }
        return schedulintCount;
    }

    @Override
    public Integer changeSchoolCreate(Grade grade, Grade gradeNew,ChangeSchoolDetail changeSchoolDetail,boolean isDelOd){
        QueryWrapper<CourseSchedulingDetail> courseSchedulingDetailQueryWrapper = new QueryWrapper<>();
        courseSchedulingDetailQueryWrapper.eq("grade_id",grade.getId());
        courseSchedulingDetailQueryWrapper.orderByAsc("id");

        Organization inSchool = iOrganizationService.getById(changeSchoolDetail.getInSchoolId());

        List<CourseSchedulingDetail> courseSchedulingDetailList = iCourseSchedulingDetailService.list(courseSchedulingDetailQueryWrapper);
        Integer count = 0;
        Staff staff = iStaffService.getById(gradeNew.getTeacherId());
        for (CourseSchedulingDetail courseSchedulingDetail:courseSchedulingDetailList){
            courseSchedulingDetail.setChangeSchoolDetailId(changeSchoolDetail.getId());

            CourseSchedulingDetail courseSchedulingDetailNew = new CourseSchedulingDetail();
            BeanUtils.copyProperties(courseSchedulingDetail,courseSchedulingDetailNew);
            courseSchedulingDetailNew.setId(idWorker.nextId());
            courseSchedulingDetailNew.setOutId(courseSchedulingDetail.getId());
            courseSchedulingDetailNew.setGradeId(gradeNew.getId());//新班级id
            courseSchedulingDetailNew.setGradeName(gradeNew.getName());//新班级id
            courseSchedulingDetailNew.setSchoolId(gradeNew.getSchoolId());//新班级id
            courseSchedulingDetailNew.setTeacherId(gradeNew.getTeacherId());//新班级id
            courseSchedulingDetailNew.setTeacherName(staff.getName());
            courseSchedulingDetailNew.setCreatedAt(LocalDateTime.now().withNano(0));
            courseSchedulingDetailNew.setSchoolName(inSchool.getName());
            iCourseSchedulingDetailService.save(courseSchedulingDetailNew);

            //删除原有数据
            //courseSchedulingDetail.setDeleted(2);
            iCourseSchedulingDetailService.updateById(courseSchedulingDetail);

            //精品课
            if (courseSchedulingDetail.getLessonType().equals(1) && isDelOd){
                iCourseSchedulingDetailService.updateDeleted(courseSchedulingDetail.getId(),2);
            }
            count++;
        }
        return count;
    }



    /**
     *
     * @param grade
     * @param schoolName
     * @param courseName
     * @param teacherName
     * @param roomName
     * @param subjectsId
     * @param subjectsName
     * @param quarterNum
     * @param nextDate
     * @param courseSchedulingId
     * @param lessonNumParam 课次
     * @return
     * @throws ParseException
     */
    public boolean addSchedulingForGrade(Grade grade, String schoolName, String courseName, String teacherName, String roomName, Long subjectsId, String subjectsName, Integer quarterNum, LocalDate nextDate, Long courseSchedulingId,Integer lessonNumParam) throws ParseException {

        /**
         * 是否存在排课信息
         */
        Long isAt = iCourseSchedulingDetailService.getIsAtForDate(grade.getId(), nextDate);
        if (isAt > 0) {
            return false;
        }

        Integer lessonNum = null;
        CourseSchedulingAlways courseSchedulingAlways = courseSchedulingAlwaysMapper.getOneForYearAndDate(grade.getYearPart(),grade.getQuarterNum(),CommonUtil.getGradeSection(grade.getYearClassId().getKey()),nextDate.toString());
        if (courseSchedulingAlways != null){
            lessonNum = courseSchedulingAlways.getNum();
        }
        Integer isVacation = grade.getIsVacation();
        //是否假期，0否，1是（假期指的是：寒假、暑假）
        if (isVacation.equals(1)) {
            if (grade.getQuarterNum().equals(3)){
                //由于暑假高中会存在两期，因此课次不按校历获取
                /*if (grade.getYearClassId().getKey() > 9){
                    lessonNum = null;
                }*/
                //暑假班，智能课与刷题班课
                if (!grade.getLessonType().equals(1)){
                    lessonNum = null;
                }
            }
        }
        if (lessonNum == null){
            lessonNum = iCourseSchedulingDetailService.getNextLessonNum(grade.getId(),nextDate);
        }else {
            Integer lessonNumNext = iCourseSchedulingDetailService.getNextLessonNum(grade.getId(),nextDate);
            if (lessonNum > lessonNumNext){
                lessonNum = lessonNumNext;
            }
        }

        CourseSchedulingDetail courseSchedulingDetail = new CourseSchedulingDetail();
        Long id = idWorker.nextId();
        courseSchedulingDetail.setId(id);
        courseSchedulingDetail.setSchoolId(grade.getSchoolId());
        courseSchedulingDetail.setCourseSchedulingId(courseSchedulingId);

        courseSchedulingDetail.setLessonNum(lessonNum);//课次
        courseSchedulingDetail.setSchoolName(schoolName);
        courseSchedulingDetail.setGradeId(grade.getId());
        courseSchedulingDetail.setGradeName(grade.getName());
        courseSchedulingDetail.setCourseId(grade.getCourseId());
        courseSchedulingDetail.setCourseName(courseName);
        courseSchedulingDetail.setSchoolDate(nextDate);
        LocalDateTime startDateTime = DateUtil.StrToDateTime(nextDate + "T" + grade.getTimeStart() + ":00");
        LocalDateTime endDateTime = DateUtil.StrToDateTime(nextDate + "T" + grade.getTimeEnd() + ":00");
        courseSchedulingDetail.setStartDateTime(startDateTime);
        courseSchedulingDetail.setEndDateTime(endDateTime);
        courseSchedulingDetail.setTimeStart(grade.getTimeStart());
        courseSchedulingDetail.setTimeEnd(grade.getTimeEnd());
        courseSchedulingDetail.setArrangingWay(3);
        courseSchedulingDetail.setTeacherId(grade.getTeacherId());
        courseSchedulingDetail.setTeacherName(teacherName);
        courseSchedulingDetail.setAssistantId(grade.getHelpTeacherId());
        courseSchedulingDetail.setClassRoomId(grade.getClassRoomId());
        courseSchedulingDetail.setClassRoomName(roomName);
        courseSchedulingDetail.setSubjectsId(subjectsId);
        courseSchedulingDetail.setSubjectsName(subjectsName);
        courseSchedulingDetail.setYearPart(grade.getYearPart());
        courseSchedulingDetail.setQuarter(quarterNum);
        courseSchedulingDetail.setLessonType(grade.getLessonType());
        iCourseSchedulingDetailService.save(courseSchedulingDetail);
        return true;
    }


    /**
     * 学期A
     *
     * @param yearPart
     * @param quarterNum
     * @param schoolId
     * @return
     */
    public List<Map<String, Object>> getSchedulingTermA(Integer yearPart, Integer quarterNum, Long schoolId, Integer subjectsId, Integer lessonType, Integer sourseLeve, Long adminId) {
        //是否假期，0否，1是（假期指的是：寒假、暑假）
        Integer IsVacation = iDictService.getIsVacation(quarterNum);
        String schedulingType = "";

        /**
         * 班级上课时间
         */
        List<SchedulingGrade> gradeList = iGradeService.getGradeListForScheduling(schoolId, subjectsId, yearPart, quarterNum, null,lessonType,sourseLeve,adminId);

        /**
         * 教室
         */
        List<ClassRoom> classRoomList = iClassRoomService.getGradeClassRoomList(schoolId, yearPart, quarterNum,lessonType,sourseLeve,adminId);

        String[] weeks = {"", "周一", "周二", "周三", "周四", "周五"};
        String column = "column";

        Map<String, Object> dynamicColumns = new LinkedHashMap<>();
        Integer i = 0;
        List<Map<String, Object>> mapList = new ArrayList<>();

        for (ClassRoom classRoom : classRoomList) {
            dynamicColumns = new LinkedHashMap<>();
            i = 0;
            for (String weeksName : weeks) {
                String weeksColumn = column + i;
                if (i.equals(0)) {
                    dynamicColumns.put(weeksColumn, classRoom.getName());

                } else {
                    List<SchedulingGrade> mapGradeList = new ArrayList<>();
                    String gradeName = "";
                    for (SchedulingGrade grade : gradeList) {
                        if (grade.getClassRoomId().equals(classRoom.getId()) && grade.getWeekDates().equals(i.toString())) {
                            //Map<String,Object> gradeMap = new HashMap<>();
                            //gradeMap.put("grade",grade);
                            mapGradeList.add(grade);
                            //gradeName += grade.getName()+",";
                            weeks[0] = grade.getSchoolTime();
                        }
                    }
                    dynamicColumns.put(weeksColumn, mapGradeList);
                    //dynamicColumns.put(weeksColumn,gradeName);
                }
                //dynamicColumns.put(weeksColumn[i], classRoom.getName());
                i++;
            }
            mapList.add(dynamicColumns);

        }
        Map<String, Object> map = new HashMap<>();
        map.put("headList", weeks);
        map.put("mapList", mapList);

        List<Map<String, Object>> rsMapList = new ArrayList<>();
        rsMapList.add(map);
        return rsMapList;
    }



    /**
     * 学期B
     *
     * @param yearPart
     * @param quarterNum
     * @param schoolId
     * @return
     */
    public List<Map<String, Object>> getSchedulingTermB(Integer yearPart, Integer quarterNum, Long schoolId, Integer subjectsId,Integer lessonType,Integer sourseLeve,Long adminId) {
        //是否假期，0否，1是（假期指的是：寒假、暑假）
        Integer IsVacation = iDictService.getIsVacation(quarterNum);
        String schedulingType = "";
        List<ClassRoom> classRoomList = iClassRoomService.getGradeClassRoomList(schoolId, yearPart, quarterNum,lessonType,sourseLeve,adminId);

        String[] weeks = {"周六", "周日"};


        List<Map<String, Object>> rsMapList = new ArrayList<>();
        for (String weekName : weeks) {
            String weekDates = "6";
            if (weekName.equals("周日")) {
                weekDates = "0";
            }
            List<SchedulingGrade> gradeList = iGradeService.getGradeListForScheduling(schoolId, subjectsId, yearPart, quarterNum, weekDates,lessonType,sourseLeve,adminId);

            String column = "column";
            List<String> schoolTimeList = iGradeService.getSchoolTimeList(schoolId, yearPart, quarterNum, null,lessonType);
            schoolTimeList.add(0, weekName);

            Map<String, Object> dynamicColumns = new LinkedHashMap<>();
            Integer i = 0;
            List<Map<String, Object>> mapList = new ArrayList<>();
            for (ClassRoom classRoom : classRoomList) {
                dynamicColumns = new LinkedHashMap<>();
                i = 0;
                for (String schoolTime : schoolTimeList) {
                    String weeksColumn = column + i;
                    if (i.equals(0)) {
                        dynamicColumns.put(weeksColumn, classRoom.getName());
                    } else {
                        List<SchedulingGrade> mapGradeList = new ArrayList<>();
                        String gradeName = "";
                        for (SchedulingGrade grade : gradeList) {
                            if (grade.getClassRoomId().equals(classRoom.getId()) && schoolTime.equals(grade.getSchoolTime())) {
                                //Map<String,Object> gradeMap = new HashMap<>();
                                //gradeMap.put("grade",grade);
                                mapGradeList.add(grade);
                                //gradeName += grade.getName()+",";
                            }
                        }
                        dynamicColumns.put(weeksColumn, mapGradeList);
                        //dynamicColumns.put(weeksColumn,gradeName);
                    }
                    i++;
                }
                mapList.add(dynamicColumns);
            }

            Map<String, Object> map = new HashMap<>();
            map.put("headList", schoolTimeList);
            map.put("mapList", mapList);
            rsMapList.add(map);
        }
        return rsMapList;
    }

    /**
     * 假期
     *
     * @param yearPart
     * @param quarterNum
     * @param schoolId
     * @return
     */
    public List<Map<String, Object>> getSchedulingVacation(Integer yearPart, Integer quarterNum, Long schoolId, Integer subjectsId,Integer lessonType,Integer sourseLeve,Long adminId) {
        //是否假期，0否，1是（假期指的是：寒假、暑假）
        Integer IsVacation = iDictService.getIsVacation(quarterNum);
        String schedulingType = "";
        List<ClassRoom> classRoomList = iClassRoomService.getGradeClassRoomList(schoolId, yearPart, quarterNum,lessonType,sourseLeve,adminId);

        String[] weeks = {"A天", "B天"};


        List<Map<String, Object>> rsMapList = new ArrayList<>();
        for (String weekName : weeks) {
            String weekDates = "A";
            if (weekName.equals("B天")) {
                weekDates = "B";
            }
            List<SchedulingGrade> gradeList = iGradeService.getGradeListForScheduling(schoolId, subjectsId, yearPart, quarterNum, weekDates,lessonType,sourseLeve,adminId);

            String column = "column";
            List<String> schoolTimeList = iGradeService.getSchoolTimeList(schoolId, yearPart, quarterNum, null,lessonType);
            schoolTimeList.add(0, weekName);

            Map<String, Object> dynamicColumns = new LinkedHashMap<>();
            Integer i = 0;
            List<Map<String, Object>> mapList = new ArrayList<>();
            for (ClassRoom classRoom : classRoomList) {
                dynamicColumns = new LinkedHashMap<>();
                i = 0;
                for (String schoolTime : schoolTimeList) {
                    String weeksColumn = column + i;
                    if (i.equals(0)) {
                        dynamicColumns.put(weeksColumn, classRoom.getName());
                    } else {
                        List<SchedulingGrade> mapGradeList = new ArrayList<>();
                        String gradeName = "";
                        for (SchedulingGrade grade : gradeList) {
                            if (grade.getClassRoomId().equals(classRoom.getId()) && schoolTime.equals(grade.getSchoolTime())) {
                                //Map<String,Object> gradeMap = new HashMap<>();
                                //gradeMap.put("grade",grade);
                                mapGradeList.add(grade);
                                //gradeName += grade.getName()+",";
                            }
                        }
                        dynamicColumns.put(weeksColumn, mapGradeList);
                        //dynamicColumns.put(weeksColumn,gradeName);
                    }
                    i++;
                }
                mapList.add(dynamicColumns);
            }


            Map<String, Object> map = new HashMap<>();
            map.put("headList", schoolTimeList);
            map.put("mapList", mapList);
            rsMapList.add(map);
        }
        return rsMapList;
    }
}
